package com.example.statistics.ui.sales

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.SalesOrder
import com.example.statistics.common.BaseLoadingListActivity
import com.example.statistics.ex.goTimeFormat
import com.example.statistics.ex.httpGet
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityForResult
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector
import com.example.statistics.ex.onActionLongClicked

class SalesStatisticsListActivity : BaseLoadingListActivity<SalesOrder>() {
    override val title: String
        get() = "销售统计"

    override val itemLayoutRes: Int
        get() = R.layout.list_item_common_display

    override val url: String
        get() = UrlConfig.SALES_STATISTICS_LIST

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        topBarRightImage {
            activityForResult<SalesStatisticsEditActivity>() {
                refresh()
            }
        }
    }

    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: SalesOrder, position: Int) {
        val str = StringBuilder()
        bean.salesProductList.forEach {
            str.append(it.factoryNo + it.productName)
                    .append(":")
                    .append(it.salesNumber)
                    .append("件*")
                    .append(it.salesPrice)
                    .append(" = ")
                    .append(it.salesTotalPrice)
                    .append("\n")

        }
        injector.text(R.id.tv_title, bean.customerName)
                .text(R.id.tv_bottom_right, bean.date.goTimeFormat())
                .text(R.id.tv_subtitle, str)
                .clicked {
                    activityForResult<SalesStatisticsEditActivity>(Constants.ID to bean.ID) {
                        refresh()
                    }
                }
                .onActionLongClicked(
                        "编辑" to {
                            activityForResult<SalesStatisticsEditActivity>(Constants.ID to bean.ID) {
                                refresh()
                            }
                        },
                        "删除" to {
                            httpGet<Any>(UrlConfig.SALES_STATISTICS_DELETE, config = {
                                listParams = listOf("id" to bean.ID)
                            }) {
                                refresh()
                            }
                        }
                )
    }


}